package sampleDataManage

import (
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/peopleNameCount"
	"github.com/flipped-aurora/gin-vue-admin/server/model/sampleDataManage"
	sampleDataManageReq "github.com/flipped-aurora/gin-vue-admin/server/model/sampleDataManage/request"
)

type SampleDataService struct {
}

// CreateSampleData 创建SampleData记录
// Author [piexlmax](https://github.com/piexlmax)
func (sampleDataService *SampleDataService) CreateSampleData(sampleData *sampleDataManage.SampleData) (err error) {
	err = global.GVA_DB.Create(sampleData).Error
	return err
}

// DeleteSampleData 删除SampleData记录
// Author [piexlmax](https://github.com/piexlmax)
func (sampleDataService *SampleDataService) DeleteSampleData(sampleData sampleDataManage.SampleData) (err error) {
	err = global.GVA_DB.Delete(&sampleData).Error
	return err
}

// DeleteSampleDataByIds 批量删除SampleData记录
// Author [piexlmax](https://github.com/piexlmax)
func (sampleDataService *SampleDataService) DeleteSampleDataByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]sampleDataManage.SampleData{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateSampleData 更新SampleData记录
// Author [piexlmax](https://github.com/piexlmax)
func (sampleDataService *SampleDataService) UpdateSampleData(sampleData sampleDataManage.SampleData) (err error) {
	err = global.GVA_DB.Save(&sampleData).Error
	return err
}

// GetSampleData 根据id获取SampleData记录
// Author [piexlmax](https://github.com/piexlmax)
func (sampleDataService *SampleDataService) GetSampleData(id uint) (sampleData sampleDataManage.SampleData, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&sampleData).Error
	return
}

// GetSampleDataInfoList 分页获取SampleData记录
// Author [piexlmax](https://github.com/piexlmax)
func (sampleDataService *SampleDataService) GetSampleDataInfoList(info sampleDataManageReq.SampleDataSearch) (list []sampleDataManage.SampleData, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&sampleDataManage.SampleData{})
	var sampleDatas []sampleDataManage.SampleData
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Symc != "" {
		db = db.Where("symc LIKE ?", "%"+info.Symc+"%")
	}
	if info.StartSysj != nil && info.EndSysj != nil {
		db = db.Where("sysj BETWEEN ? AND ? ", info.StartSysj, info.EndSysj)
	}
	if info.Sywcr != "" {
		db = db.Where("sywcr LIKE ?", "%"+info.Sywcr+"%")
	}
	if info.Sywj != "" {
		db = db.Where("sywj LIKE ?", "%"+info.Sywj+"%")
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	var OrderStr string
	orderMap := make(map[string]bool)
	orderMap["ybsjid"] = true
	if orderMap[info.Sort] {
		OrderStr = info.Sort
		if info.Order == "descending" {
			OrderStr = OrderStr + " desc"
		}
		db = db.Order(OrderStr)
	}

	err = db.Limit(limit).Offset(offset).Find(&sampleDatas).Error
	return sampleDatas, total, err
}

// GetPeopleNameCountList 统计实验人出现次数
func (sampleDataService *SampleDataService) GetPeopleNameCountList() (list []peopleNameCount.PeopleNameCount, err error) {

	//err = global.GVA_DB.Raw("select sywcr AS Sywcr,count(*) from public.\"Tsample_data\" where deleted_at is NULL group by sywcr").Scan(&list).Error
	err = global.GVA_DB.Raw("select creator AS Creator,count(*) from public.\"exa_file_upload_and_downloads\" where deleted_at is NULL and tag != 'dir' group by creator ").Scan(&list).Error
	fmt.Println(list)
	//err = global.GVA_DB.Raw("select ybsjid from public.\"Tsample_data\" where deleted_at is NULL").Find(&list).Error
	//for i := range list {
	//	fmt.Println("更新list内容如下：%s %d", list[i].Sywcr, list[i].Count)
	//
	//	//fmt.Println("list内容如下：%s %d", list[i].Ybshid, list[i].Count)
	//}
	return list, err

}

// FindByIds 批量查询，用于批量导出
// Author [piexlmax](https://github.com/piexlmax)
func (sampleDataService *SampleDataService) FindByIds(ids request.IdsReq) (list []sampleDataManage.SampleData, total int64, err error) {
	//err = global.GVA_DB.Delete(&[]hostingPlatform.Platform{}, "id in ?", ids.Ids).Error
	// 创建db
	//这里需要修改
	db := global.GVA_DB.Model(&sampleDataManage.SampleData{})
	var platforms []sampleDataManage.SampleData
	db = db.Where("id in ?", ids.Ids)

	err = db.Count(&total).Error
	err = db.Find(&platforms).Error

	return platforms, total, err
}
